package com.ruoyi.operation.order.enter.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.operation.order.enter.domain.EnterForm;
import com.ruoyi.operation.order.enter.service.IEnterFormService;
import com.ruoyi.operation.order.product.domain.Product;
import com.ruoyi.operation.order.product.service.IProductService;
import com.ruoyi.operation.order.shop.domain.Shop;
import com.ruoyi.operation.order.shop.service.IShopService;
import com.ruoyi.operation.order.total.domain.Total;
import com.ruoyi.operation.order.wwebchat.domain.Wwebchat;
import com.ruoyi.operation.order.wwebchat.service.IWwebchatService;
import com.ruoyi.project.system.dept.domain.Dept;
import com.ruoyi.project.system.dept.service.IDeptService;
import com.ruoyi.project.system.role.service.IRoleService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author king
 * @create 2019/4/12 13:44
 */
@Controller
@RequestMapping(value = "/enter/enter")
public class EnterFormController extends BaseController {

    // 定义一个跳转到页面的路径
    private String prefix = "order/enter/";

    @Autowired
    private IProductService iProductService;

    @Autowired
    private IEnterFormService iEnterFormService;

    @Autowired
    private IRoleService iRoleService;

    @Autowired
    private IUserService iUserService;

    @Autowired
    private IDeptService iDeptService;
    @Autowired
    private IShopService shopService;

@Autowired
private IWwebchatService wwebchatService;
    /**
     * 跳转到订单详情的页面
     * @return
     */
    @RequiresPermissions(value = "enter:enter:enter")
    @GetMapping()
    String enter(){
        return prefix + "enter";
    }

    @RequestMapping(value = "/list")
    @RequiresPermissions(value = "enter:enter:enter")
    @ResponseBody
    public TableDataInfo allEnertForm(EnterForm enterForm){

        // 获取当前登陆人的id
        String getUserId = ShiroUtils.getLoginId();
        // 更具当前登陆人获取roleId
        List<String> getRoleId = iRoleService.getRoleId(getUserId);
        for (String s : getRoleId){
            if (s.equals("1") || s.equals("5")){
                // 此时是超级管理员和鹰之队的管理员
                enterForm.setType(" 1");
            }else if(s.equals("4")){
                // 此时是登陆人
                enterForm.setType("3");
                enterForm.setCreateBy(getUserId);
            }else{
                enterForm.setType("2");
                enterForm.setDeptId(iUserService.getDeptId(getUserId));
            }
        }

        startPage();
        List<EnterForm> enterFormList = iEnterFormService.allEnterFormList(enterForm);
        return getDataTable(enterFormList);

    }

    @RequestMapping(value = "/getroleid")
    @ResponseBody
    public List<String> selectByroleid(){
        // 获取当前登陆人的id
        String getUserId = ShiroUtils.getLoginId();
        // 更具当前登陆人获取roleId
        List<String> getRoleId = iRoleService.getRoleId(getUserId);
        return getRoleId;
    }

    /**
     * 跳转到录入信息的页面
     * @return
     */
    @RequestMapping(value = "/add")
    @RequiresPermissions("enter:enter:add")
    String add(){
        return prefix + "add";
    }


    /**
     * 录入订单信息
     * @param enterForm
     * @return
     */
    @RequestMapping(value = "/saveEnterForm")
    @RequiresPermissions("enter:enter:add")
    @ResponseBody
    public AjaxResult saveEnterForm(EnterForm enterForm){
        Wwebchat wwebchat=new Wwebchat();
        String wwNum = enterForm.getWwNum();
        if ( wwNum != null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(wwNum);
            wwNum = m.replaceAll("");
            enterForm.setWwNum(wwNum);
        }

            wwebchat.setWebChat(enterForm.getWebChatNum());
            wwebchat.setWwNum(enterForm.getWwNum());
            wwebchatService.insertWwebchat(wwebchat);

        return toAjax(iEnterFormService.saveEnterForm(enterForm));
    }

    /**
     * 编辑订单信息
     * @return
     */
    @RequestMapping(value = "/edit/{id}")
    @RequiresPermissions("enter:enter:edit")
    String edit(@PathVariable("id") String id, Model model,EnterForm enterForm){
        if (null != "id" || id != ""){
            enterForm = iEnterFormService.getEnterFormById(id);
        }
        // 获取所有的平台的信息
        List<Dept> deptList = iDeptService.allDeptList();
        //获取对应平台所获取的店铺信息
//        List<Product> shopList=iProductService.selectByDeptId(enterForm.getEnterDeptId());
        List<Shop> shopList = shopService.selectByDeptId(enterForm.getEnterDeptId());
        Product product = new Product();
        product.setProductShopId(enterForm.getEnterShopId());
        // 获取对应的平台获取所用的产品信息
        List<Product> productList = iProductService.allProductList(product);
        model.addAttribute("enterForm",enterForm);
        model.addAttribute("productList",productList);
        model.addAttribute("deptList",deptList);
        model.addAttribute("shopList",shopList);
        return prefix + "edit";
    }


    /**
     * 编辑订单信息
     * @param enterForm
     * @return
     */
    @RequestMapping(value = "editEnterForm")
    @RequiresPermissions(value = "enter:enter:edit")
    @ResponseBody
    public AjaxResult editEnterForm(EnterForm enterForm){
        return toAjax(iEnterFormService.editEnterForm(enterForm));
    }

    /**
     * 删除订单信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/remove/{id}")
    @RequiresPermissions(value = "enter:enter:remove")
    @ResponseBody
    public AjaxResult delEnterForm(@PathVariable("id") String id){
        return toAjax(iEnterFormService.delEnterForm(id));
    }

    /**
     * 导入信息
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "录入管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("enter:enter:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {

        ExcelUtil<Wwebchat> util = new ExcelUtil<Wwebchat>(Wwebchat.class);
        List<Wwebchat> wwebchatList = util.importExcel(file.getInputStream());
        String message = wwebchatService.importWwebchat(wwebchatList, updateSupport);
        return AjaxResult.success(message);
    }


    /**
     * 新导入信息
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "录入管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("enter:enter:newImport")
    @PostMapping("/newImportData")
    @ResponseBody
    public AjaxResult newImportData(MultipartFile file, boolean updateSupport) throws Exception
    {

        ExcelUtil<EnterForm> util = new ExcelUtil<EnterForm>(EnterForm.class);
        List<EnterForm> enterFormList = util.importExcel(file.getInputStream());
        Iterator<EnterForm> sListIterator = enterFormList.iterator();
        while (sListIterator.hasNext()) {
            EnterForm str = sListIterator.next();
            if (str.getShopName().equals("")) {
                sListIterator.remove();
            }
        }
        String message = iEnterFormService.importEnterForm(enterFormList, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 下载模板
     * @return
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<EnterForm> util = new ExcelUtil<EnterForm>(EnterForm.class);
        return util.importTemplateExcel("录入信息");
    }
    /**
     * 查询所有的产品信息
     * @return
     */
    @RequestMapping(value = "/allProduct")
    @ResponseBody
    public List<Product> allProduct(String deptId){

        deptId=iUserService.getDeptId(ShiroUtils.getLoginId());
        return iProductService.allProductByDeptId(deptId);
    }

    /**
     * 根据平台查询店铺
     * @param deptId
     * @return
     */
    @RequestMapping(value = "/allShop")
    @ResponseBody
    public List<Shop> allShop(String deptId){
        return shopService.selectByDeptId(deptId);
    }

    /**
     *根据店铺查询产品
     * @param shopId
     * @return
     */
    @RequestMapping(value = "/product")
    @ResponseBody
    public List<Product> selectByshopid(String deptId,String shopId){
        return iProductService.selectByShopId(deptId,shopId);
    }

    /**
     * 验证订货单编号是否存在
     * @param formNum
     * @return
     */
    @RequestMapping(value = "checkFormNum")
    @ResponseBody
    public String checkFormNum(String formNum){
        return iEnterFormService.allFormNum(formNum);
    }

    /**
     * 查询所有的平台
     * @return
     */
    @RequestMapping(value = "/allDeptT")
    @ResponseBody
    public List<Dept> allDeptT(){
        return iDeptService.allDeptList();
    }

    /**
     * 查询所有的录单员
     * @return
     */
    @RequestMapping(value = "getUserByRoeId")
    @ResponseBody
    public List<User> getUserByRoeId(){
        return iUserService.getUserByRoleId();
    }


    /**
     * 根据旺旺号查寻存在的订单
     * @param wwNum
     * @return
     */
    @RequestMapping(value = "/allListEnter")
    @ResponseBody
    public List<EnterForm> allListEnter(String wwNum){
        return iEnterFormService.allListEnter(wwNum);
    }


    @RequestMapping(value = "/selectByWWechat")
    @ResponseBody
    public List<EnterForm> selectByWWechat(EnterForm enterForm){
        return iEnterFormService.selectByWWechat(enterForm);
    }

    /**
     * 导出订单信息
     * @param enterForm
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/exportExcel")
    @RequiresPermissions(value = "total:total:exportExcel" )
    public void exportExcel(EnterForm enterForm, HttpServletRequest request, HttpServletResponse response) throws Exception{
        iEnterFormService.exportTotal(enterForm,request,response);
    }

    /**
     * 批量删除录入信息
     * @param ids
     * @return
     */
    @RequiresPermissions(value = "enter:enter:remove")
    @Log(title = "录入管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(iEnterFormService.batchDeleteIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
}
